package com.commands;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("serial")
public class CalculateServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		HttpSession session = req.getSession();

		/* Name (0), division (1), grade (2), final exam percent (3), current overall percent (4), goal percent (5),
		 * project hours (6), research hours (7), reading hours (8), other hours (9), confidence (10), finals day (11).
		 */
		Vector<Vector<String>> classes = (Vector<Vector<String>>)session.getAttribute("classes");

		// Assumption that classes is not empty.
		int num_classes = Integer.parseInt((String)session.getAttribute("num_classes"));

		int num_ld = 0;
		int num_ud = 0;
		int num_grade = 0;
		int num_pass = 0;
		for (int i=0; i<classes.size(); i++) {
			if (Integer.parseInt(classes.get(i).get(1)) == 0) num_ld++;
			else num_ud++;
			if (Integer.parseInt(classes.get(i).get(2)) == 0) num_grade++;
			else num_pass++;
		}

		// Partial grade and partial pass are percents (i.e. 25%).
		double partial_grade = 0;
		double partial_pass = 0;
		if (num_pass > 0) {
			partial_pass = 0.2 * (100 / num_pass);
			partial_grade = 0.8 * (100 / num_grade);
		}
		else {
			partial_grade = 1 / num_classes;
		}

		// Sets quarter or semester value.
		int system = Integer.parseInt((String)session.getAttribute("system"));
		if (system == 0) system += 10;
		else system += (Integer.parseInt((String)session.getAttribute("semester")) - 1);

		double[] unscaled_data = new double[num_classes];
		double unscaled_total = 0;
		for (int j=0; j<classes.size(); j++) {
			int confidence = Integer.parseInt(classes.get(j).get(10));
			int division = Integer.parseInt(classes.get(j).get(1));
			int project_hours = Integer.parseInt(classes.get(j).get(6));
			int research_hours = Integer.parseInt(classes.get(j).get(7));
			int reading_hours = Integer.parseInt(classes.get(j).get(8));
			int other_hours = Integer.parseInt(classes.get(j).get(9));
			int spent_total_hours = project_hours + research_hours + reading_hours + other_hours;

			// Sets total hours needed to study for a given class.
			int calculated_total_hours;
			if (division == 0) calculated_total_hours = system * 3;
			else calculated_total_hours = system * 6;

			double final_exam_percent = Double.parseDouble(classes.get(j).get(3));
			double current_overall_percent = Double.parseDouble(classes.get(j).get(4));
			double goal_percent = Double.parseDouble(classes.get(j).get(5));

			// 0.01 (10) + 0.1 (1) + (1 - 0.8 * (120 / 240)) = 0.8
			double part_one = 0.01 * confidence + 0.1 * division + (1 - 0.8 * (spent_total_hours / calculated_total_hours));
			// 0.01 (25) + 0.005 ((90 - 80) / (0.01 * 25)) = 0.45
			double part_two = 0.01 * final_exam_percent + 0.005 * ((goal_percent - current_overall_percent) / (0.01 * final_exam_percent));

			double partial;
			if (Integer.parseInt(classes.get(j).get(2)) == 0) partial = partial_pass;
			else partial = partial_grade;
			// 25 + (25 * 0.5 * (0.8)) + (25 * 0.5 * (0.45)) = 40.625
			double computed_proportion = partial + (partial * 0.5 * (part_one)) + (partial * 0.5 * (part_two));

			unscaled_data[j] = computed_proportion;
			unscaled_total += computed_proportion;
		}

		double[] scaled_data = new double[num_classes];
		for(int k=0; k<classes.size(); k++) {
			double partial;
			if (Integer.parseInt(classes.get(k).get(2)) == 0) partial = partial_pass;
			else partial = partial_grade;
			scaled_data[k] = (partial / 100) * unscaled_data[k];
		}

		session.setAttribute("scaled_data", scaled_data);

	}
}
